//
//  FontScale.swift
//  HeadlineNews
//
//  Created by han on 2019/7/29.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

struct SizeScale {
    // 针对不同设备的缩放
    enum ScreenScaleType: Int {
        case none = 0// 不缩放
        case equalRatio = 1// 等比缩放  20/375*414
        case equalDifference = 2// 等差缩放 20 + (414 - 375)
    }
    
    // 针对同一个设备手动调缩放等级
    enum ScaleType {
        case none // 不缩放
        case ratioDifference // 等差缩放 20 + (414 - 375)
    }
    
    // 默认对比屏幕是375的屏幕。
    var baseWidth: CGFloat = 375
    var currentWidth: CGFloat = CommonSize.deviceWidth
    
    var source: CGFloat // 初始数据
    var target: CGFloat? // 手动改变字体大小后数据，可以为空,此时手动设置时，不会改变字体
    
    var maximum: CGFloat? // 用于区分不同数据设备
    var minimum: CGFloat?
    
    var screenScaleType: ScreenScaleType = .none
    var scaleType: ScaleType = .ratioDifference
    
    init(source: CGFloat, target: CGFloat?, maximum: CGFloat?, mininum: CGFloat?) {
        self.source = source
        self.target = target
        self.maximum = maximum
        self.minimum = mininum
        if target == nil {
            self.scaleType = .none
        }
    }
    
    // 将数据，按屏幕适配
    mutating func performScreenScale(_ value: CGFloat) -> CGFloat {
        var destinate = value
        if screenScaleType == .equalDifference || screenScaleType == .equalRatio {
            if screenScaleType == .equalDifference {
                destinate = value + (currentWidth - baseWidth)
            } else {
                destinate = value / baseWidth * currentWidth
            }
            if let maximum = maximum {
                if destinate > maximum { destinate = maximum }
            }
            if let mininum = minimum {
                if destinate < mininum { destinate = mininum }
            }
        }
        return destinate
    }
    
    // 手动改变字体大小
    mutating func performScale(level: Int, levelCount: Int) -> CGFloat {
        source = performScreenScale(source)
        guard scaleType == .ratioDifference, var target = target else { return source }
        target = performScreenScale(target)
        let div = (target - source) / CGFloat(levelCount)
        return source + CGFloat(level) * div
    }
}

// font
//extension SizeScale {
//    static systemFont
//}

struct FontScale {
    
}

extension FontScale: ExpressibleByArrayLiteral {
    init(arrayLiteral elements: CGFloat...) {
        
    }
}

extension FontScale {
//    static func systemFont(ofSize sizes: CGFloat...) -> FontScale {
//
//    }
}


/*
 open class func systemFont(ofSize fontSize: CGFloat) -> UIFont
 
 open class func boldSystemFont(ofSize fontSize: CGFloat) -> UIFont
 
 open class func italicSystemFont(ofSize fontSize: CGFloat) -> UIFont
 
 init?(name fontName: String, size fontSize: CGFloat)
 */



